#include "broker.h"

#include <signal.h>

static sig_atomic_t running = 1;

static void stop_broker(int sig)
{
    running = 0;
}

int main(int argc, char *argv[])
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    if (argc < 3) {
        fprintf(stderr, "Usage: %s <primary> <id> [options]\n", argv[0]);
        exit(0);
    }
    // set_trace_level(T_INFO);
    std::vector<std::string> options;
    for (int i = 3; i < argc; i++) {
        options.push_back(argv[i]);
    }

    cvmfs_broker *broker;
    try {
        broker = new cvmfs_broker(argv[1], argv[2], options);
    } catch (const std::string &e) {
        TERR("Broker failed to start: %s", e.c_str());
        exit(1);
    }

    struct sigaction sigint_act;
    sigint_act.sa_handler = stop_broker;
    sigint_act.sa_flags = 0;
    sigemptyset(&sigint_act.sa_mask);
    sigaction(SIGINT, &sigint_act, NULL);

    while (running) {
        sleep(1);
    }

    delete broker;

    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

